//
//  RPNetworkUitls.m
//  RPCFCADemo_Example
//
//  Created by john on 2023/5/5.
//  Copyright © 2023 john. All rights reserved.
//

#import "RPNetworkUitls.h"

@implementation RPNetworkUitls
+ (AFHTTPSessionManager *)getNetworkManager
{
    NSString *baseURLString = RPHOST_URL;
    NSURL *baseURL = [NSURL URLWithString:baseURLString];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
//    manager.securityPolicy = [self getSecurityPolicy];;
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.securityPolicy = [self getSecurityPolicy];
//    manager.securityPolicy = [AFSecurityPolicy defaultPolicy];
//    manager.securityPolicy.allowInvalidCertificates = YES;
//    [manager.securityPolicy setValidatesDomainName:NO];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];// 响应
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", @"text/html", nil];
//    manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
//    manager.securityPolicy=[self getSecurityPolicy];
    return manager;
}

+ (AFSecurityPolicy *)getSecurityPolicy
{
    //    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"server_bin" ofType:@"cer"];
    //    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"server" ofType:@"cer"];
    //    NSData *cerData = [NSData dataWithContentsOfFile:cerPath];
    //    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    //    securityPolicy.allowInvalidCertificates = YES;
    //    securityPolicy.validatesDomainName = NO;
    //    securityPolicy.pinnedCertificates = [[NSSet alloc] initWithObjects:cerData, nil];
    //    return securityPolicy;
    
    
    //先导入证书，找到证书的路径
    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"cer"];
    NSData *certData = [NSData dataWithContentsOfFile:cerPath];
    
    NSSet * certSet = [[NSSet alloc] initWithObjects:certData, nil];
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    // 是否允许,NO-- 不允许无效的证书
    [securityPolicy setAllowInvalidCertificates:YES];
    // 设置证书
    [securityPolicy setPinnedCertificates:certSet];
    securityPolicy.validatesDomainName = NO;
    
//    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//    manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json", @"text/html",@"text/json", @"text/javascript",@"text/plain", nil];
//    //关闭缓存避免干扰测试
//    manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
//    manager.securityPolicy=securityPolicy;
    return securityPolicy;
}

@end
